import random
import uuid
from suds import WebFault
from Autotests.Suites.StressTest.Operations.CommonOperations import CommonOperations
from Config.RunConfig import RunConfig, Options


class NotificationOperations:
    def __init__(self):
        self._operationsList = [
            self._addNotificationRule,
            self._addNotificationTransport,
            self._addNotificationTemplate,
            self._enableNotificationRule,
            self._enumerateAllNotificationTransports,
            self._enumerateActiveNotificationRules,
            self._getNotificationTemplate,
            self._getNotificationTransport,
            self._getPartnerNotificationTemplate
        ]
        if  RunConfig().get(Options.StressTest.stressAllPartners) == 'True':
            self._operationsList.append(self._modifyContactPerson)
            self._operationsList.append(self._modifyCustomColumn)
            self._operationsList.append(self._modifyNotificationRule)
            self._operationsList.append(self._modifyNotificationTemplate)
            self._operationsList.append(self._modifyNotificationTransport)
            self._operationsList.append(self._removeNotificationTransport)
            self._operationsList.append(self._removeNotificationTemplate)
            self._operationsList.append(self._removeNotificationRule)
            self._operationsList.append(self._removeCustomColumn)
            self._operationsList.append(self._removeContactPerson)

        self._transportLimit = 5

    def _getRandomOperation(self):
        return random.choice(self._operationsList)

    def _addNotificationRule(self, manserv, partnerId):
        manserv.AddNotificationRule(id = random.randint(1, 10000), name = uuid.uuid4(), entity = 'Account',
            partnerId = partnerId, expression = uuid.uuid4(), predicate = 'New', schedule = uuid.uuid4(),
            transportType = 'Smtp', templateId = random.randint(1, 1000), recipients = uuid.uuid4(),
            enabled = 'true', revision = random.randint(1, 1000))

    def _addNotificationTransport(self, manserv, partnerId):
        transportsCount = len(self._enumerateAllNotificationTransports(manserv, partnerId).notificationTransports)
        if transportsCount < self._transportLimit:
            manserv.AddNotificationTransport(id = random.randint(1, 10), partnerId = partnerId, type = 'Smtp',
                                             parameters = uuid.uuid4())

    def _addNotificationTemplate(self, manserv, partnerId):
        manserv.AddNotificationTemplate(id = random.randint(1, 1000), templateId = random.randint(1, 1000),
            name = uuid.uuid4(), partnerId = partnerId, body = uuid.uuid4())

    def _enableNotificationRule(self, manserv, partnerId):
        manserv.EnableNotificationRule(id = random.randint(1, 1000), partnerId = partnerId,
            enable = random.choice(['true', 'false']))

    def _getNotificationTemplate(self, manserv, partnerId):
        manserv.GetNotificationTemplate(id = random.randint(1, 1000))

    def _getNotificationTransport(self, manserv, partnerId):
        manserv.GetNotificationTransport(id = random.randint(1, 1000))

    def _getPartnerNotificationTemplate(self, manserv, partnerId):
        manserv.GetPartnerNotificationTemplate(templateId = random.randint(1, 1000), partnerId = partnerId)

    def _enumerateAllNotificationTransports(self, manserv, partnerId):
        return manserv.EnumerateAllNotificationTransports()

    def _enumerateActiveNotificationRules(self, manserv, partnerId):
        manserv.EnumerateActiveNotificationRules()

    def _modifyContactPerson(self, manserv, partnerId):
        manserv.ModifyContactPerson(id = random.randint(1, 1000), partnerId = random.randint(1, 1000),
                                   name = uuid.uuid4(), phoneNumber = random.randint(1, 1000))

    def _modifyCustomColumn(self, manserv, partnerId):
        manserv.ModifyCustomColumn(id = random.randint(1, 1000), partnerId = random.randint(1, 1000),
                                   valueType = 'String', name = uuid.uuid4())

    def _modifyNotificationRule(self, manserv, partnerId):
        manserv.ModifyNotificationRule(id = random.randint(1, 1000), name = uuid.uuid4(), entity = 'Account',
                                       partnerId = random.randint(1, 1000),  expression = uuid.uuid4(),
                                       predicate = 'New', schedule = uuid.uuid4(), transportType = 'Smtp',
                                       templateId = random.randint(1, 1000), recipients = uuid.uuid4(),
                                       enabled = 'true', revision = random.randint(1, 1000))

    def _modifyNotificationTemplate(self, manserv, partnerId):
        manserv.ModifyNotificationTemplate(id = random.randint(1, 1000), templateId = random.randint(1, 1000),
                                           name = uuid.uuid4(), partnerId = random.randint(1, 1000),
                                           body = uuid.uuid4())

    def _modifyNotificationTransport(self, manserv, partnerId):
        manserv.ModifyNotificationTransport(id = random.randint(1, 1000), partnerId = random.randint(1, 1000))

    def _needRemove(self):
        return random.randint(1, 1000) % CommonOperations._baseGrowthRate == 0

    def _removeNotificationTransport(self, manserv, partnerId):
        if self._needRemove():
            manserv.RemoveNotificationTransport(id = random.randint(1, 5))

    def _removeNotificationTemplate(self, manserv, partnerId):
        if self._needRemove():
            manserv.RemoveNotificationTemplate(id = random.randint(1, 1000))

    def _removeNotificationRule(self, manserv, partnerId):
        if self._needRemove():
            manserv.RemoveNotificationRule(id = random.randint(1, 1000))

    def _removeCustomColumn(self, manserv, partnerId):
        if self._needRemove():
            manserv.RemoveCustomColumn(customColumnId = random.randint(1, 1000))

    def _removeContactPerson(self, manserv, partnerId):
        if self._needRemove():
            manserv.RemoveContactPerson(contactPersonId = random.randint(1, 1000))

